<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');
//公司
class Orderlist_model extends MY_Model
{
    private $table = 'orderlist';

    public function __construct()
    {
        parent::__construct($this->table);
    }

    public function getList()
    {
        $data = [];
        if ($this->input->get('payTitle', TRUE)) {
            $this->db->like('orderlist.payTitle', $this->input->get('payTitle', TRUE));
        }
        if ($this->input->get('trade_no', TRUE)) {
            $data['orderlist.trade_no'] = $this->input->get('trade_no', TRUE);
        }
        if ($this->input->get('realName', TRUE)) {
            $this->db->like('user.realName', $this->input->get('realName', TRUE));
        }
        if ($this->input->get('sevriceNo', TRUE)) {
            $data['technician.sevriceNo'] = $this->input->get('sevriceNo', TRUE);
        }
        if ($this->input->get('status', TRUE)) {
            $data['orderlist.status'] = $this->input->get('status', TRUE);
        }
        $this->_join('user', 'user.userId = orderlist.userId', 'left');
       /* $this->_join('project', 'project.projectId = orderlist.listId', 'left');*/
        $this->_join('technician', 'technician.technicianId = orderlist.technicianId', 'left');
        $this->_get_db($data);

        $this->db->select('COUNT(*) as count, SUM(totalPrice) as sum');
        $info = $this->db->get('orderlist')->row_array();

        $data['limit'] = $this->input->get('limit', TRUE);
        $data['page'] = $this->input->get('page', TRUE);
        if ($this->input->get('realName', TRUE)) {
            $this->db->like('user.realName', $this->input->get('realName', TRUE));
        }
        if ($this->input->get('payTitle', TRUE)) {
            $this->db->like('orderlist.payTitle', $this->input->get('payTitle', TRUE));
        }
        $this->db->order_by('orderlist.createTime', 'DESC');
        $this->db->order_by('orderlist.updateTime', 'DESC');
        $this->db->select('orderlist.orderId, orderlist.payTitle, orderlist.trade_no, user.realName,'
            . 'technician.sevriceNo, orderlist.status, orderlist.createTime, orderlist.listId,'
            . 'user.phone, technician.realName as technicianName, orderlist.typeFlag, orderlist.price,'
            . 'orderlist.num, orderlist.totalPrice, orderlist.totalPrice, orderlist.createTime, orderlist.updateTime');
        $this->_get_db($data);
        $info['data'] = $this->db->get('orderlist')->result_array();

        foreach ($info['data'] as &$value) {
            if ($value['createTime'] != '0' && $value['createTime'] !== NULL) {
                $value['createTime'] = date('Y-m-d  H:i:s', $value['createTime']);
            }
            if ($value['updateTime'] != '0' && $value['updateTime'] != null) {
                $value['updateTime'] = date('Y-m-d  H:i:s', $value['updateTime']);
            }

            if ($value['typeFlag'] == 1) {
                $value['typeFlag'] = '项目';
                if($value['listId']) {
                    $this->db->where('projectId', $value['listId']);
                    $this->db->select('title');
                    $project = $this->db->get('project')->row_array();
                    $value['title'] = $project['title'];
                }else {
                    $value['title'] = null;
                }
            } else if (($value['typeFlag'] == 2)) {
                $value['typeFlag'] = '给技师送礼物';
                $value['title'] = null;
            }

            if ($value['status'] == 1) {
                $value['status'] = '未付款';
            } else if ($value['status'] == 2) {
                $value['status'] = '已付款未使用';
            } else if ($value['status'] == 3) {
                $value['status'] = '已使用未评价';
            } else if ($value['status'] == 4) {
                $value['status'] = '已评价';
            }
        }

        return $info;
    }

    public function getProjectList($companyId)
    {
        $data = [];
        $data['project.companyId'] = $companyId;
        $data['orderlist.typeFlag'] = 1;
        if ($this->input->get('payTitle', TRUE)) {
            $this->db->like('orderlist.payTitle', $this->input->get('payTitle', TRUE));
        }
        if ($this->input->get('trade_no', TRUE)) {
            $data['orderlist.trade_no'] = $this->input->get('trade_no', TRUE);
        }
        if ($this->input->get('realName', TRUE)) {
            $this->db->like('user.realName', $this->input->get('realName', TRUE));
        }
        if ($this->input->get('sevriceNo', TRUE)) {
            $data['technician.sevriceNo'] = $this->input->get('sevriceNo', TRUE);
        }
        if ($this->input->get('status', TRUE)) {
            $data['orderlist.status'] = $this->input->get('status', TRUE);
        }
        $this->_join('user', 'user.userId = orderlist.userId', 'left');
        $this->_join('project', 'project.projectId = orderlist.listId', 'left');
        $this->_join('technician', 'technician.technicianId = orderlist.technicianId', 'left');
        $this->_get_db($data);

        $this->db->select('COUNT(*) as count');
        $info = $this->db->get('orderlist')->row_array();

        $data['limit'] = $this->input->get('limit', TRUE);
        $data['page'] = $this->input->get('page', TRUE);
        if ($this->input->get('realName', TRUE)) {
            $this->db->like('user.realName', $this->input->get('realName', TRUE));
        }
        if ($this->input->get('payTitle', TRUE)) {
            $this->db->like('orderlist.payTitle', $this->input->get('payTitle', TRUE));
        }
        $this->db->order_by('orderlist.createTime', 'DESC');
        $this->db->order_by('orderlist.updateTime', 'DESC');
        $this->db->select('orderlist.orderId, orderlist.payTitle, orderlist.trade_no, user.realName,'
            . 'technician.sevriceNo, orderlist.status, orderlist.createTime, orderlist.listId,'
            . 'user.phone, technician.realName as technicianName, orderlist.typeFlag, orderlist.price,'
            . 'orderlist.num, orderlist.totalPrice, orderlist.totalPrice, orderlist.createTime,'
            . 'orderlist.updateTime, project.title');
        $this->_get_db($data);
        $info['data'] = $this->db->get('orderlist')->result_array();

        foreach ($info['data'] as &$value) {
            if ($value['createTime'] != '0' && $value['createTime'] !== NULL) {
                $value['createTime'] = date('Y-m-d  H:i:s', $value['createTime']);
            }
            if ($value['updateTime'] != '0' && $value['updateTime'] !== NULL) {
                $value['updateTime'] = date('Y-m-d  H:i:s', $value['updateTime']);
            }else {
                $value['updateTime'] = '未付款';
            }

            if ($value['status'] == 1) {
                $value['status'] = '未付款';
            } else if ($value['status'] == 2) {
                $value['status'] = '已付款未使用';
            } else if ($value['status'] == 3) {
                $value['status'] = '已使用未评价';
            } else if ($value['status'] == 4) {
                $value['status'] = '已评价';
            }
        }

        return $info;
    }

    public function getTechnicianList($companyId)
    {
        $data = [];
        $data['technician.companyId'] = $companyId;
        $data['orderlist.typeFlag'] = 2;
        if ($this->input->get('payTitle', TRUE)) {
            $this->db->like('orderlist.payTitle', $this->input->get('payTitle', TRUE));
        }
        if ($this->input->get('trade_no', TRUE)) {
            $data['orderlist.trade_no'] = $this->input->get('trade_no', TRUE);
        }
        if ($this->input->get('realName', TRUE)) {
            $this->db->like('user.realName', $this->input->get('realName', TRUE));
        }
        if ($this->input->get('sevriceNo', TRUE)) {
            $data['technician.sevriceNo'] = $this->input->get('sevriceNo', TRUE);
        }
        if ($this->input->get('status', TRUE)) {
            $data['orderlist.status'] = $this->input->get('status', TRUE);
        }
        $this->_join('user', 'user.userId = orderlist.userId', 'left');
        $this->_join('technician', 'technician.technicianId = orderlist.listId', 'left');
        $this->_get_db($data);

        $this->db->select('COUNT(*) as count');
        $info = $this->db->get('orderlist')->row_array();

        $data['limit'] = $this->input->get('limit', TRUE);
        $data['page'] = $this->input->get('page', TRUE);
        if ($this->input->get('realName', TRUE)) {
            $this->db->like('user.realName', $this->input->get('realName', TRUE));
        }
        if ($this->input->get('payTitle', TRUE)) {
            $this->db->like('orderlist.payTitle', $this->input->get('payTitle', TRUE));
        }
        $this->db->order_by('orderlist.createTime', 'DESC');
        $this->db->order_by('orderlist.updateTime', 'DESC');
        $this->db->select('orderlist.orderId, orderlist.payTitle, orderlist.trade_no, user.realName,'
            . 'technician.sevriceNo, orderlist.status, orderlist.createTime, orderlist.listId,'
            . 'user.phone, technician.realName as technicianName, orderlist.typeFlag, orderlist.price,'
            . 'orderlist.num, orderlist.totalPrice, orderlist.totalPrice, orderlist.createTime,'
            . 'orderlist.updateTime, technician.nickName as title');
        $this->_get_db($data);
        $info['data'] = $this->db->get('orderlist')->result_array();

        foreach ($info['data'] as &$value) {
            if ($value['createTime'] != '0' && $value['createTime'] !== NULL) {
                $value['createTime'] = date('Y-m-d  H:i:s', $value['createTime']);
            }
            if ($value['updateTime'] != '0'  || $value['updateTime'] != null) {
                $value['updateTime'] = date('Y-m-d  H:i:s', $value['updateTime']);
            }

            if ($value['status'] == 1) {
                $value['status'] = '未付款';
            } else if ($value['status'] == 2) {
                $value['status'] = '已付款未使用';
            } else if ($value['status'] == 3) {
                $value['status'] = '已使用未评价';
            } else if ($value['status'] == 4) {
                $value['status'] = '已评价';
            }
        }

        return $info;
    }

    public function getUserOrder($where,$limit,$offset){
        $select='orderlist.orderId, orderlist.price, orderlist.num, orderlist.totalPrice, orderlist.createTime, orderlist.status, refund.status as refundStatus,
                   concat("'.IMAGEHOST.'", company.logo) as logo, company.name, project.title, project.projectId, technician.realName as technicianName';

        if($where){
            if(isset($where['orderlist.status'])){
                if($where['orderlist.status']==5){      //退款查询
                    $this->db->where('refund.status->=',0);
                    unset($where['orderlist.status']);
                }
                $this->db->where($where);
            }
        }

        $this->db->join('project','project.projectId=orderlist.listId');
        $this->db->join('company','project.companyId=company.companyId');
        $this->db->join('technician','technician.technicianId=orderlist.technicianId','left');
        $this->db->join('refund','refund.orderid=orderlist.orderId','left');
        $dbArray['page']=['limit'=>$limit,'offset'=>$offset];
        return $this->getWhere($where,$select,$dbArray);
    }

    //获取订单详情
    public function getDetail($orderId,$userId){
        $where=['orderlist.orderId'=>$orderId,'orderlist.userId'=>$userId];
        $select='orderlist.orderId,orderlist.price,orderlist.num,orderlist.totalPrice,orderlist.createTime,orderlist.status,orderlist.trade_no,refund.status as refundStatus,
                   concat("'.IMAGEHOST.'", company.logo) as logo,company.name,company.address,company.phone,
                   concat("'.IMAGEHOST.'", project.poster) as projectPoster,project.title,project.projectId,technician.realName as technicianName';
        $this->db->join('project','project.projectId=orderlist.listId');
        $this->db->join('company','project.companyId=company.companyId');
        $this->db->join('technician','technician.technicianId=orderlist.technicianId','left');
        $this->db->join('refund','refund.orderid=orderlist.orderId','left');
        return $this->getWhereRow($where,$select);
    }

    //技师当日收到的礼物列表数据
    public function getGiftListToday($technicianId){
        $startTime=strtotime(date('Ymd'));       //当天的开始时间
        $endTime=$startTime+24*60*60;              //当天的结束时间
        $where=['orderlist.typeFlag'=>2,'orderlist.listId'=>$technicianId,'orderlist.status'=>2,'orderlist.createTime>='=>$startTime,'orderlist.createTime<='=>$endTime];
        return $this->getGiftData($where);
    }
    //技师当日收到的礼物列表数据
    public function getGiftList($technicianId,$dbArray=[]){
        $startTime=strtotime(date('Ymd'));       //当天的开始时间
        $endTime=$startTime+24*60*60;              //当天的结束时间
        $where=['orderlist.typeFlag'=>2,'orderlist.listId'=>$technicianId,'orderlist.status'=>2,'orderlist.reateTime->='=>$startTime,'orderlist.createTime<='=>$endTime];
        return $this->getGiftData($where,$dbArray);
    }
    //获取技师礼物数据
    private function getGiftData($where,$dbArray=[]){
        $this->db->join('gift','orderlist.listId=gift.giftId');
        $this->db->join('user','orderlist.userId=user.userId');
        $select='gift.*,concat("'.IMAGEHOST.'",gift.url) as url,orderlist.createTime,orderlist.price,orderlist.num,orderlist.totalPrice,
                   user.userId,concat("'.IMAGEHOST.'",user.avatar) as avatar,user.nickName,user.realName';
        return $this->getWhere($where,$select,$dbArray);
    }
}